<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Draw.io Viewer</title>
<style type="text/css">
html, body {
	height:100%;
	overflow:hidden;
}
body {
	background-color:#ffffff;
	background-image:url(/images/drawlogo-gray.svg);
	background-repeat:no-repeat;
	background-position:center top;
	background-size: 64px;
	font-family:Arial,sans-serif;
	width:100%;
	margin:0;
}
</style>
</head>
<body>
<script type="text/javascript">
// Parses URL parameters
function getUrlParam(param)
{
	var result = (new RegExp(param + '=([^&]*)')).exec(window.location.search);
	
	if (result != null && result.length > 0)
	{
		return decodeURIComponent(result[1].replace(/\+/g, '%20'))
	}
	
	return null;
};

function getBaseUrl()
{
	var baseUrl = getUrlParam('xdm_e', true) + getUrlParam('cp', true);
	//Ensure baseUrl belongs to attlasian (*.jira.com and *.atlassian.net)
	//Since we add cp to xdm_e, we had to ensure that there is a slash after the domain. Since if xdm_e is ok, cp can corrupt is such as cp = '.fakedomain.com' such that baseUrl is atlassian.net.fakedomain.com
	if (/^https:\/\/([^\.])+\.jira\.com\//.test(baseUrl + '/') || /^https:\/\/([^\.])+\.atlassian\.net\//.test(baseUrl + '/')) 
	{
		return baseUrl;
	}
	throw 'Invalid baseUrl!';
};

// Sets global environment variables
RESOURCE_BASE = '/resources/dia';
STENCIL_PATH = '/stencils';
SHAPES_PATH = '/shapes';

</script>
<script type="text/javascript" src="/js/viewer.min.js"></script>
<script type="text/javascript">
(function()
{
	// Enables dynamic loading of shapes and stencils (same domain)
	mxStencilRegistry.dynamicLoading = true;

	// Loads the Atlassian API
	var script = document.createElement('script');
	var baseUrl = getBaseUrl();

	// Loads the attachment and renders the diagram
	var diagramWidth = parseFloat(getUrlParam('width'));
	var diagramHeight = parseFloat(getUrlParam('height'));
	var diagramName = getUrlParam('diagramName');
	
	//ceoId and owningPageId are IDs of the page that potentially hold the attachment
	//they will differ when page history is shown, ceoId will be historical version ID,
	//owningPageId will be the ID of the current version that holds the attachment
	//ceoId is used as fallback in case owningPageId is not set(should be very rare)
	var ceoId = getUrlParam('ceoId');
	var owningPageId = getUrlParam('owningPageId');
	var revision = null;
	var tbStyle = getUrlParam('tbstyle') || 'top';
	var links = getUrlParam('links') || 'auto';
	var tbHeight = tbStyle == 'top' ? GraphViewer.prototype.toolbarHeight : 0;
	var zoom = parseFloat(getUrlParam('zoom') || 1);
	var border = 0;
	
	document.body.style.backgroundImage = 'url(/images/aui-wait.gif)';
	document.body.style.backgroundPosition = 'left top';
	document.body.style.backgroundSize = 'auto auto';
	
	function main()
	{
		// Sets initial placeholder size to allow for scrollbars in fit to page width
		AP.resize('100%', (diagramHeight * zoom + tbHeight + 2 * border));
		
		function showError(msg)
		{
			document.body.style.backgroundImage = 'none';
			document.body.style.padding = '4px';
			mxUtils.write(document.body, msg);
			AP.resize('100%', 24);
		};
		
		AP.require(['request', 'dialog', 'messages', 'navigator'], function(request, dialog, messages, navigator)
		{
			// Uses pageId from current page as page in macro may be outdated after export
			navigator.getLocation(function (data)
		    {
				var candidateId = (owningPageId != null && owningPageId.length > 0) ? owningPageId : ceoId;

				if (data != null && data.target != null && data.context != null)
				{
					candidateId = data.context.contentId;
				}
				
				// Loads the given XML into the viewer
				function showDiagram(id, backupId, name, revision, page, links, retryParams)
				{
					id = id.toString();
					
					retryParams = retryParams || {}; //so we can use it without NPE check
					
					if (id != null && id.length > 0 && name != null && name.length > 0) 
					{
						document.body.style.backgroundImage = 'none';
						var img = document.createElement('img');
						img.style.cssText = 'max-width:100%;';
						img.setAttribute('src', baseUrl + '/download/attachments/' + id + '/'
								+ encodeURIComponent(name) + ".png?api=v2"
								+ (revision != null ? "&version=" + revision : ""));
						
						if (zoom != 1)
						{
							img.style.width = Math.round(diagramWidth * zoom) + 'px';
						}
						
						document.body.appendChild(img);
					}
					else
					{
						showError('Error: Invalid descriptor');
					}
				};
	
				var myPageId = (owningPageId != null && owningPageId.length > 0) ? owningPageId : ceoId;
				showDiagram(candidateId, (owningPageId != null && owningPageId.length > 0) ? owningPageId : ceoId, diagramName, revision, null, links);
		    });
		});
	};
	
	mxResources.loadDefaultBundle = false;
	var bundle = mxResources.getDefaultBundle(RESOURCE_BASE, mxLanguage) ||
		mxResources.getSpecialBundle(RESOURCE_BASE, mxLanguage);

	// Prefetches asynchronous requests so that below code runs synchronous
	// Loading the correct bundle (one file) via the fallback system in mxResources. The stylesheet
	// is compiled into JS in the build process and is only needed for local development.
	var bundleLoaded = false;
	var scriptLoaded = false;
	var validSize = document.documentElement.offsetWidth > 0;

	function mainBarrier()
	{
		if (validSize && bundleLoaded && scriptLoaded)
		{
			main();
		}
	};

	// Disables delayed rendering since the container is created on the fly
	GraphViewer.prototype.checkVisibleState = false;
	
	// Workaround for collapsed panel is to delay main until size is not 0
	if (!validSize)
	{
		var listener = function()
		{
			if (document.documentElement.offsetWidth > 0)
			{
				window.removeEventListener('resize', listener);
				validSize = true;
				mainBarrier();
			}
		};
		
		window.addEventListener('resize', listener);
	}
	
	mxUtils.getAll([bundle], function(xhr)
	{
		// Adds bundle text to resources
		mxResources.parse(xhr[0].getText());
		bundleLoaded = true;
		mainBarrier();
	});

	script.onload = function()
	{
		// Workaround for Google Chrome triggering
		// no resize event if height is set to 0
		if (mxClient.IS_GC && !validSize)
		{
			AP.resize('100%', 1);
		}
		
		scriptLoaded = true;
		mainBarrier();
	};

	script.src = 'https://connect-cdn.atl-paas.net/all.js';
	script.setAttribute('data-options', 'sizeToParent:true;');
	document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>
